package com.cheng.servlet;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

@Controller
public class uploadServlet {
    @RequestMapping(value = "upload")/*文件上传*/
    public String upload(@RequestParam("name")String name, @RequestParam("file") MultipartFile file) throws IOException {
        System.out.println("描述信息："+name);
        //将文件上传至服务器的某一个硬盘文件中
        InputStream inputStream = file.getInputStream();//拿到该文件的输入流，再通过输出流输出到硬盘文件中
        String filename = file.getOriginalFilename();
        //输出流
        OutputStream outputStream = new FileOutputStream("E:\\Cheng\\学习\\项目\\JAVA\\spring mvc\\spring mvc项目\\"+filename);
        byte [] bs=new byte[1024];
        int len=-1;
        while ((len=inputStream.read(bs))!=-1){
            outputStream.write(bs,0,len);
        }
        inputStream.close();
        outputStream.close();
        System.out.println("上传成功！");
        return "city";
    }
}
